#include "stdafx.h"
#include "CheckBoxExample.h"

FCView* CheckBoxExample::createView(xmlNodePtr node, const String& type){
if (type == L"mycheckbox")
   {
       return new MyCheckBox();
   }
   return FCUIXml::createView(node, type);
}
void CheckBoxExample::loadFile(const String& fileName, FCView *view){
    FCUIXml::loadFile(fileName, view);
    //获取第二页
    FCTabPage *tabPage = getTabPage(L"TabPage2");
    FCCheckBox *checkBox = new FCCheckBox();
    FCPoint point = {100,100};
    checkBox->setLocation(point);
    FCSize size = {100,20};
    checkBox->setSize(size);
    tabPage->addView(checkBox);
    checkBox->setText(L"选项");
    checkBox->setChecked(true);

    //从Xml中获取按钮
    FCCheckBox *button1 = getCheckBox(L"CheckBox");
    FCCheckBox *button2 = getCheckBox(L"CheckBox2");
    FCCheckBox *button3 = getCheckBox(L"CheckBox3");
    FCCheckBox *button4 = getCheckBox(L"CheckBox4");

    //绑定选项改变事件
    button1->addEvent((FCEventCallBack*)this, FCEventID_CheckedChanged, this);
    button2->addEvent((FCEventCallBack*)this, FCEventID_CheckedChanged, this);
    button3->addEvent((FCEventCallBack*)this, FCEventID_CheckedChanged, this);
    button4->addEvent((FCEventCallBack*)this, FCEventID_CheckedChanged, this);
    checkBox->addEvent((FCEventCallBack*)this, FCEventID_CheckedChanged, this);
}

void CheckBoxExample::callEvent(String eventName, Object sender, Object invoke){
    FCCheckBox *checkBox = (FCCheckBox*)sender;
}

MyCheckBox::MyCheckBox(){
    FCSize buttonSize = {30, 14};
    m_buttonSize = buttonSize;
    m_borderColor = FCColor_None;
    m_currentX = -1;
    m_timerID = FCView::getNewTimerID();

}

MyCheckBox::~MyCheckBox(){
    stopTimer(m_timerID);
}

void MyCheckBox::onRemove(){
    FCCheckBox::onRemove();
    stopTimer(m_timerID);
}

void MyCheckBox::onClick(FCTouchInfo touchInfo){
    bool oldChecked = isChecked();
    FCCheckBox::onClick(touchInfo);
    bool newChecked = isChecked();
    if (oldChecked != newChecked)
    {
        if (oldChecked)
        {
            m_currentX = (getButtonSize().cx - 22);
            startTimer(m_timerID, 20);

        }
        else
        {
            m_currentX = -(getButtonSize().cx - 22);
            startTimer(m_timerID, 20);
        }
    }
}

void MyCheckBox::onTimer(int timerID){
    FCCheckBox::onTimer(timerID);
    if (m_timerID == timerID)
    {
        int aimLeft = 0;
        if (m_currentX != -1)
        {
            int xSub = aimLeft - m_currentX;
            if (xSub != 0)
            {
                if (xSub > 0)
                {
                    m_currentX += 2;
                }
                else if (xSub < 0)
                {
                    m_currentX -= 2;
                }
                if (std::abs(aimLeft - m_currentX) < 3)
                {
                    m_currentX = aimLeft;
                    stopTimer(m_timerID);
                }
                invalidate();
            }
        }
    }
}

void MyCheckBox::onPaintCheckButton(FCPaint *paint, const FCRect& clipRect){
    FCRect drawRect = clipRect;
    int width = getWidth(), height = getHeight();
    bool iChecked = isChecked();
    int d = getButtonSize().cy;
    int pHeight = 10;
    int round = 4;
    FCRect pRect = {1, height / 2 - pHeight / 2, clipRect.right - 1, height / 2 + pHeight / 2};
    Long backColor = FCColor::rgba(0, 0, 0, 25); ;
    Long backColor1 = FCColor::rgb(150, 150, 150);
    FCRect ellipseRect = {1, (height - d) / 2, 1 + d, (height + d) / 2};
    if (iChecked)
    {
        backColor = FCColor::rgba(42, 138, 195, 200);
        backColor1 = FCColor::rgb(42, 138, 195);
        ellipseRect.left = clipRect.right - 1 - d;
        ellipseRect.right = clipRect.right - 1;
    }
    if (m_currentX != -1)
    {
        ellipseRect.left += m_currentX;
        ellipseRect.right += m_currentX;
    }
    paint->fillRoundRect(backColor, pRect, round);
    paint->fillEllipse(backColor1, ellipseRect);
}

void MyCheckBox::onPaintBorder(FCPaint *paint, const FCRect& clipRect){
}

void MyCheckBox::onPaintForeground(FCPaint *paint, const FCRect& clipRect){
    String text = getText();
    int width = getWidth(), height = getHeight();
    if (width > 0 && height > 0)
    {
        FCRect buttonRect = {5, (height - m_buttonSize.cy) / 2, 5 + m_buttonSize.cx, (height + m_buttonSize.cy) / 2};
        //绘制背景图
        onPaintCheckButton(paint, buttonRect);
        //绘制文字
        if ((int)text.length() > 0)
        {
            FCFont *font = getFont();
            FCSize tSize = paint->textSize(text, font, -1);
            FCPoint tLocation = {buttonRect.right + 5, (height - tSize.cy) / 2};
            FCRect tRect = {tLocation.x, tLocation.y, tLocation.x + tSize.cx, tLocation.y + tSize.cy};
            Long textColor = getPaintingTextColor();
            paint->drawText(text, textColor, font, tRect, -1);
        }
    }
}
